package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 找到所有菜单
     *
     * @return {@link Result}
     */
    @GetMapping
    public Result findAll() {
        try {
            List<Menu> result = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 分页查询
     *
     * @param currentPage 当前页面
     * @param pageSize    页面大小
     * @return {@link Result}
     */
    @GetMapping("page")
    public Result findByPage(@RequestParam("currentPage") Integer currentPage,
                             @RequestParam("pageSize") Integer pageSize
                            ) {
        try {
            PageResult result = menuService.findByPage(currentPage, pageSize);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 添加菜单
     *
     * @param permissionIds ids允许
     * @return {@link Result}
     */
    @PostMapping
    public Result addMenu(@RequestParam("permissionIds") Integer[] permissionIds, @RequestBody Menu menu){
        try {
            menuService.addMenu(permissionIds,menu);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 通过id找到菜单
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("{id}")
    public Result findMenuById(@PathVariable("id") String id){
        try {
            Menu result = menuService.findMenuById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 通过菜单id找到对应的权限
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("{id}/permissions")
    public Result findPermissionByMenuId(@PathVariable("id") Integer id){
        try {
             Integer[] result = menuService.findPermissionByMenuId(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 更新菜单
     *
     * @param permissionIds ids允许
     * @param menu          菜单
     * @return {@link Result}
     */
    @PutMapping()
    public Result updateMenu(@RequestParam("permissionIds") Integer[] permissionIds,@RequestBody Menu menu){
        try {
            menuService.updateMenu(permissionIds,menu);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 删除权限的id
     *
     * @param id id
     * @return {@link Result}
     */
    @DeleteMapping("{id}")
    public Result deleteMenuById(@PathVariable("id") Integer id){
        try {
            menuService.deleteMenuById(id);
            return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
    }


}
